package com.uit.gis.pojo;

import processing.data.JSONObject;

import com.uit.gis.util.JsonData;
import com.uit.gis.util.Util.SpatialType;

public class Circle extends Shape {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	JSONObject circleObj = new JSONObject();
	int ellipseX, ellipseY, radius, clickCount;

	public void setup() {
		background(255);
		initialize();
		spatialType = SpatialType.CIRCLE;
	}

	@Override
	public void draw() {
		if (ellipseX > 0 && ellipseY > 0 && clickCount != 0) {
			background(255);
			stroke(0);
			// fill(175);
			radius = mouseX - ellipseX;
			ellipse(ellipseX, ellipseY, radius, radius);
		}
	}

	void initialize() {
		clickCount = 0;
		ellipseX = -1;
		ellipseY = -1;
		radius = -1;
	}

	@Override
	public void mousePressed() {
		if (clickCount == 0) {
			ellipseX = mouseX;
			ellipseY = mouseY;
			clickCount++;
		} else {
			circleObj.setInt("x", ellipseX);
			circleObj.setInt("y", ellipseY);
			circleObj.setInt("r", radius);

			initialize();
		}
	}

	@Override
	public void mouseClicked(processing.event.MouseEvent e) {

		if (e.getButton() == processing.core.PConstants.RIGHT) {

			saveToTextFile();
			continueDraw = false;
		}

	}

	@Override
	public void saveToTextFile() {
		// TODO Auto-generated method stub
		JSONObject circle = new JSONObject();
		circle.setInt("x1", circleObj.getInt("x") - circleObj.getInt("r"));
		circle.setInt("y1", circleObj.getInt("y"));
		circle.setInt("x2", circleObj.getInt("x"));
		circle.setInt("y2", circleObj.getInt("y") - circleObj.getInt("r"));
		circle.setInt("x3", circleObj.getInt("x") + circleObj.getInt("r"));
		circle.setInt("y3", circleObj.getInt("y"));
		saveJSONObject(circle, getSavingPath());
		super.saveToTextFile();
	};

	@Override
	public JsonData loadDataFromFile() {
		// TODO Auto-generated method stub
		JSONObject jsonObject = loadJSONObject(getSavingPath());
		JsonData jsonData = new JsonData(null, jsonObject);
		return jsonData;
	}

}
